import time
from libs.base.action import Action
from lxml import etree


class Shenghuo(Action):
    """
    封装爱生活小程序相关的操作
    """

    def __init__(self, driver):
        self.driver = driver

    def click_video(self):
        """点击首页视频"""
        self.find_by_xpath("//wx-button[@class='sub_btn']").click()
        time.sleep(5)

    def click_share_btn(self):
        """点击列表页分享按钮"""
        self.find_by_xpath("//wx-view[text()='分享']").click()
        time.sleep(1)

    def click_comment_btn(self):
        """点击评论按钮"""
        self.find_by_xpath("//wx-view[@id='BottomBarCommentBtn']").click()
        time.sleep(2)

    def click_share_playing_page(self):
        """点击播放页的分享按钮"""
        self.find_by_xpath("//span[text()='评论']/../../following-sibling::wx-view[1]/wx-button").click()
        time.sleep(1)

    def input_comment(self, comments):
        """输入评论信息"""
        self.find_by_xpath("//div[text()='写评论...']").click()
        time.sleep(1)
        self.switch_context_to_native()  # 输入框为安卓原生控件，需要先切换context后再操作
        edit = self.find_by_xpath("//android.widget.EditText[@text='写评论...']")
        edit.send_keys(comments)
        time.sleep(0.5)
        edit.click()
        self.switch_context_to_webview()
        time.sleep(0.5)

    def click_send_btn(self):
        """点击发送评论按钮"""
        self.find_by_xpath("//wx-view[@class='btn-send enable' and text()='发送']").click()
        # self.find_by_xpath("//android.view.View[@content-desc='发送']").click()
        time.sleep(2)

    def get_comments_number(self):
        """获取评论数"""
        locater = "//span[text()='条评论']/../preceding-sibling::wx-text[1]/span[@style='display:none;']"
        element = self.xpath_in_page_source(locater)
        comments_num = element[0].text
        if not comments_num:
            raise Exception("没有获取到评论数，可能是切换的handle不对！")
        return comments_num

    def get_comments(self):
        """获取所有评论"""
        comments = {}
        locater = "//wx-view[@class='comment-info']//span[@style='display:none;']"
        elements = self.xpath_in_page_source(locater)
        for index in range(int(len(elements) / 5)):
            comments[index + 1] = {}
            comments[index + 1]["nickname"] = elements[index * 5].text
            comments[index + 1]["praise"] = elements[index * 5 + 1].text
            comments[index + 1]["comment"] = elements[index * 5 + 2].text
            comments[index + 1]["time"] = elements[index * 5 + 3].text
        print(comments)
        return comments

    # 频道页
    def get_topic_list(self):
        """获取频道页数据"""
        topics = []
        source_code = self.driver.page_source
        locater="//wx-view[@class='topic_list']/wx-view[@class='item']"
        topic_numbers = len(etree.HTML(source_code).xpath(locater))  # 所有话题数量

        topic_name = etree.HTML(source_code).xpath("//wx-view[@class='txt_box']//wx-view[@class='txt']/text()")  # 话题标题集合
        topic_btn = etree.HTML(source_code).xpath("//wx-view[@class='txt_box']//wx-view[@class='btn']/text()")  # 话题数量集合
        for index in range(0, topic_numbers):
            topics.append({topic_name[index], topic_btn[index]})
        return topics

    # ============================================
    # 安卓原生控件

    def click_tab(self, tab='最新'):
        """切换爱生活底部tab"""
        locater = "//android.widget.TextView[@text='%s']/../.." % tab
        self.find_by_xpath(locater).click()
        time.sleep(2)

    def click_close_btn(self):
        """点击小程序右上角关闭按钮"""
        self.find_by_xpath("//android.widget.TextView[@text='糖豆爱生活']/../../following-sibling::android.widget.LinearLayout[1]/child::node()[2]").click()

    def click_more_btn(self):
        """点击小程序右上角 ...按钮"""
        self.find_by_xpath("//android.widget.TextView[@text='糖豆爱生活']/../../following-sibling::android.widget.LinearLayout[1]/child::node()[1]").click()


